<?php
namespace App\Http\Controllers\Admin;

use Crypt;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Service\AdministratorService;
use App\Http\Requests\AdminRequest;
use App\Models\Role;

class AdministratorController extends CommentController
{
	private $_administrator;
    private $_roleModel;

	public function __construct()
	{
		$this->_administrator = AdministratorService::getInstance();
	    $this->_roleModel = Role::getInstance();
    }

	public function index(Request $request)
	{	
		$list = $this->_administrator->getAdministratorList();
		$level = $this->getSessionData()['level'];
		return view('admin.administrator.index',['list'=>$list,'level'=>$level]);
	}

    /**
     * 添加页面
     * @author 朱旭
     */
	public function create()
    {
        $role = $this->_roleModel->getRoleList();
        
        return view('admin.administrator.create',compact("role"));
    }

    /**
     * 添加方法
     * @author 朱旭
     */
    public function store(AdminRequest $request)
    {
        $admin = $this->_administrator->addToAdministrator($request);
        
        return $this->returnJson($admin);
    }

     /**
     * 显示指定文章
     *
     * @param int $id
     * @return Response
     */
    public function show($id)
    {
        
    }

    /**
     * 显示编辑指定文章的表单页面
     *
     * @param int $id
     * @return Response
     */
    public function edit($id)
    {
        if ($id == 1) {
            return redirect()->action('Admin\AdministratorController@index')->withErrors(['您正在试图修改超级管理员信息']); 
        }
    	$data = $this->_administrator->getAdministratorInformation($id);
        $role = $this->_roleModel->getRoleList();
        return view("admin.administrator.edit", ['data'=>$data,'role'=>$role]);
    }

    /**
     * 在存储器中更新指定文章
     *
     * @param Request $request
     * @param int $id
     * @return Response
     */
    public function update(AdminRequest $request, $id)
    {
        if ($id == 1) {
            return redirect()->action('Admin\AdministratorController@index')->withErrors(['您正在试图修改超级管理员信息']); 
        }

        $result = $this->_administrator->editToAdministrator($request, $id);
        return $this->returnJson($result);
    }

    /**
     * 从存储器中移除指定文章
     *
     * @param int $id
     * @return Response
     */
    public function destroy($id)
    {
        if ($id == 1) {
            return redirect()->action('Admin\AdministratorController@index')->withErrors(['您正在试图移除超级管理员']); 
        }
        return $this->returnJson($this->_administrator->delete($id));
    }

    /**
     * 更改状态
     * @author 朱旭
     */
    public function status($id)
    {
        if ($id == 1) {
            return redirect()->action('Admin\AdministratorController@index')->withErrors(['您正在试图修改超级管理员信息']); 
        }
        return $this->returnJson($this->_administrator->status($id));
    }
}